// model/Message.kt

@Serializable
data class Message(
  val text: String,
  val username: String,
  val timestamp: Long,
  @BsonId
  val id: String = ObjectId().toString(), // Come from mongodb
)

// MessageDataSource.kt
// the mongodb datasource

interface MessageDataSource {
  suspend fun getAllMessages(): List<Message>

  suspend fun insertMessage(message: Message)
}

// MessageDataSourceImpl.kt

class MessageDataSourceImpl : MessageDataSource {
  private val client = KMongo.createClient().coroutine
  private val database = client.getDatabase("ChatDB")
  // auto create
  private val messages = database.getCollection<Message>()

  override suspend fun getAllMessages(): List<Message> {
    return messages.find().toList()
  }

  override suspend fun insertMessage(message: Message) {
    messages.insertOne(message)
  }
}
